﻿using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using NB.MGJ.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace NB.MGJ.SignalR
{
    public static class SignalR_MessagePush
    {
        private static Queue<L.EntityModel.Sys_PushMessage> queue { get; set; }

        /// <summary>
        /// 设置推消息 用于消息轮循
        /// </summary>
        /// <param name="id">接收人ID</param>
        /// <param name="_func">FUNC</param>
        /// <param name="_autoPush">完成后是否直接推送，默认false</param>
        public static void SetMessageQueue(Func<List<L.EntityModel.Sys_PushMessage>> _func, bool _autoPush = false)
        {
            bool isLoop = true;
            Task.Run(() =>
            {
                while (isLoop)
                {
                    var _result = _func();
                    
                    if (_result != null)
                    {
                        isLoop = false;
                        SetMessageQueue(_result);
                        if (_autoPush)
                        {
                            TriggerPush();
                        }
                    }
                    Thread.Sleep(2000);//当前线程休眠两秒
                }
            });
        }

        /// <summary>
        /// 设置推消息 用于消息轮循
        /// </summary>
        /// <param name="id">接收人ID</param>
        /// <param name="_func">FUNC</param>
        /// <param name="_autoPush">完成后是否直接推送，默认false</param>
        public static void SetMessageQueue(Func<L.EntityModel.Sys_PushMessage> _func, bool _autoPush = false)
        {
            bool isLoop = true;
            Task.Run(() =>
            {
                while (isLoop)
                {
                    var _result = _func();
                    if (_result != null)
                    {
                        isLoop = false;
                        SetMessageQueue(_result);
                        if (_autoPush)
                        {
                            TriggerPush();
                        }
                    }
                    Thread.Sleep(2000);//当前线程休眠两秒
                }
            });
        }

        /// <summary>
        /// 设置推消息
        /// </summary>
        /// <param name="id">接收人ID</param>
        /// <param name="content">消息内容</param>
        public static void SetMessageQueue(L.EntityModel.Sys_PushMessage pushInfo)
        {
            if (pushInfo == null) { throw new ArgumentNullException("Sys_PushMessage缺失"); }
            if (queue == null)
            {
                queue = new Queue<L.EntityModel.Sys_PushMessage>();
            }
            queue.Enqueue(pushInfo);
        }

        /// <summary>
        /// 设置推消息
        /// </summary>
        /// <param name="id">接收人ID</param>
        /// <param name="content">消息内容</param>
        public static void SetMessageQueue(List<L.EntityModel.Sys_PushMessage> pushInfo)
        {
            if (queue == null)
            {
                queue = new Queue<L.EntityModel.Sys_PushMessage>();
            }
            foreach (var item in pushInfo)
            {
                queue.Enqueue(item);
            }
        }

        /// <summary>
        /// 触发推送
        /// </summary>
        public static void TriggerPush()
        {
            while (queue != null && queue.Count > 0)
            {
                MessagePush(queue.Dequeue());
            }
        }

        /// <summary>
        /// 推送
        /// </summary>
        private static void MessagePush(L.EntityModel.Sys_PushMessage _pushinfo)
        {
            if (string.IsNullOrWhiteSpace(_pushinfo.PushContent))
            {
                return;
            }
            IHubContext context = GlobalHost.ConnectionManager.GetHubContext<SignalRHub>();
            context.Clients.All.SendMessage(JsonConvert.SerializeObject(_pushinfo));
        }
    }

    /// <summary>
    /// 发送方式：广播
    /// 注意点：消息风暴
    /// 吕
    /// </summary>
    [HubName("ab403fada0dc4c19ba2beb47f375f8ef")]
    public class SignalRHub : Hub
    {
        private void SendMessage(L.EntityModel.Sys_PushMessage _pushinfo)
        {
            Clients.All.SendMessage(JsonConvert.SerializeObject(_pushinfo));
        }

        public override Task OnConnected()
        {
            //SendMessage("欢迎使用码管家云系统", "欢迎使用码管家云系统");
            SignalR_MessagePush.TriggerPush();
            return base.OnConnected();
        }
    }
}